import { Card } from 'element-ui'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [

    ]
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/login.vue')
  },{
    path: '/Dialog',
    name: 'Dialog',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Dialog.vue')
  },
 
]
const router = new VueRouter({
  // mode:'history',  //history模式，打包上线时，需要服务器配合配置如果错误时挨边index.html
  routes
})
import { fn } from '@/utils/routeList.js' //把生成列表路由放到公共方法里

// //生成动态路由-登录后获取到路由列表，调用递归函数通过router.addRoute('路由里name名',值)生成动态路由
console.log('路由先走');
function loadRoute() {
  let token = localStorage.getItem('token')
  let menusList = JSON.parse(localStorage.getItem('menusList'))
  //注意此处是登录后,有了左侧导航数据，再进行路由表动态加载
  if (token && menusList) {
    let newList = fn(menusList)
    console.log('路由表结构', newList);
    newList.forEach(item => {
      router.addRoute("/", item)
    })
  }
}
//!!!注意此处在次调用1次动态路由,为了防止登录后跳转其它页面,刷新页面找不到问题。第2点注意，要在路由实例化后调用执行。
loadRoute()

// // 白名单-意思是不需要token，就可直接进入的页面
// const whiteList = ['/login', '/test', '/menu']

// //路由拦截-1.先看看有没有登录过token值，如果登录过放行 2.没有登录在次判断是否在登录页面

// router.beforeEach((to, from, next) => {
//   //有token或没token
//   let token = localStorage.getItem('token')
//   //有token放行
//   if (token) {
//     next()
//   }
//   //注意这里没token又分2种情况 1.在登录页/或者其它不不需要token页面放行 2.否则去登录页
//   else {
//     // if(to.path=="/login"){
//     if (whiteList.indexOf(to.path) != -1) {
//       next()
//     }
//     else {
//       next('/login')
//     }
//   }
// })


export default router
